﻿using Dapper.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using YouWei.Api.Common;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Models;
using YouWei.Infrastructure.Service;

namespace YouWei.Api.Controllers
{
    /// <summary>
    /// 车主微信群
    /// </summary>
    public class CarGroupController : BaseApiController
    {
        private CarGroupService _carGrupService = new CarGroupService();
        private WxGroupService _wxGroupService = new WxGroupService();
        string FileUploadUrl = Utility.ConfigHelper.GetAppSettingValue("FileUploadUrl");

        /// <summary>
        /// 获取群组列表
        /// </summary>
        /// <param name="PageSize">页大小</param>
        /// <param name="PageIndex">页码</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<Page<CarGroup>> GetCarGroups(int PageSize, int PageIndex)
        {
            var data = _carGrupService.Pages(PageSize, PageIndex);
            foreach (var item in data.rows)
            {
                item.LogoUrl = string.Format("{0}/{1}", FileUploadUrl, item.LogoUrl);
                item.HeadUrl = string.Format("{0}/{1}", FileUploadUrl, item.HeadUrl);
                item.QrCodeUrl = string.Format("{0}/{1}", FileUploadUrl, item.QrCodeUrl);
            }
            return ResultData<Page<CarGroup>>.Success(data);
        }

        /// <summary>
        /// 获取群组列表
        /// </summary>
        /// <param name="conditon">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<Page<CarGroup>> GetCarGroupsV2(CarGroupSearchModel conditon)
        {
            var data = _carGrupService.Pages(conditon);
            foreach (var item in data.rows)
            {
                item.LogoUrl = string.Format("{0}/{1}", FileUploadUrl, item.LogoUrl);
                item.HeadUrl = string.Format("{0}/{1}", FileUploadUrl, item.HeadUrl);
                item.QrCodeUrl = string.Format("{0}/{1}", FileUploadUrl, item.QrCodeUrl);
            }
            return ResultData<Page<CarGroup>>.Success(data);
        }

        /// <summary>
        /// 获取群组详情
        /// </summary>
        /// <param name="groupId">群组id</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<CarGroup> GetCarGroup(int groupId)
        {
            CarGroup group = _carGrupService.GetCarGroup(groupId);
            if (group != null)
            {
                group.LogoUrl = string.Format("{0}/{1}", FileUploadUrl, group.LogoUrl);
                group.HeadUrl = string.Format("{0}/{1}", FileUploadUrl, group.HeadUrl);
                group.QrCodeUrl = string.Format("{0}/{1}", FileUploadUrl, group.QrCodeUrl);
            }

            return ResultData<CarGroup>.Success(group);
        }

        /// <summary>
        /// 根据id获取群组
        /// </summary>
        /// <param name="groupIds"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<List<CarGroup>> CarGroups(string groupIds)
        {
            List<CarGroup> groups = _carGrupService.GetCarGroups(groupIds);
            return ResultData<List<CarGroup>>.Success(groups);
        }

        /// <summary>
        /// 根据群组获取微信群信息
        /// </summary>
        /// <param name="carGroupId"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<List<WxGroup>> GetWxGroups(int carGroupId)
        {
            var groups = _wxGroupService.GetWxGroups(carGroupId);
            foreach (var item in groups)
            {
                item.QrCodeUrl = string.Format("{0}/{1}", FileUploadUrl, item.QrCodeUrl);
            }
            return ResultData<List<WxGroup>>.Success(groups);
        }
    }
}
